package com.loveyukari.controller;


import com.loveyukari.model.Auth;
import com.loveyukari.service.AuthService;
import com.loveyukari.service.PermissionService;
import com.loveyukari.support.JsonResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Autowired
    private AuthService authService;

    @Autowired
    private PermissionService permissionService;


    @RequestMapping(value = "/addAuth",method = RequestMethod.POST)
    public JsonResult add(int id,int permissionId){
        Auth auth = authService.getCacheAuth(id);
        if (auth == null){
            return new JsonResult(1,"用户不存在");
        }
        permissionService.addAuthPermission(auth.getId(),permissionId);
        return new JsonResult(0,"",auth.getId());
    }

    @RequestMapping(value = "/addAuthName",method = RequestMethod.POST)
    public JsonResult add(String name,int permissionId){
        Auth auth = authService.findAuthByName(name);
        if (auth == null){
            return new JsonResult(1,"用户不存在");
        }
        permissionService.addAuthPermission(auth.getId(),permissionId);
        return new JsonResult(0,"",auth.getId());
    }

    @RequestMapping(value = "/deleteAuth",method = RequestMethod.POST)
    public JsonResult delete(int authId,int permissionId){
        permissionService.deleteAuthPermission(authId,permissionId);
        return new JsonResult(0,"",authId);
    }

    @RequestMapping(value = "/getAuth")
    public JsonResult getPermissionAuth(int permissionId){
        List<Integer> authIdList = permissionService.getAuthIdByPermission(permissionId);
        ArrayList<Auth> result = new ArrayList<>();
        for (Integer authId :
                authIdList) {
            Auth auth = authService.getCacheAuth(authId);
            if (auth != null){
                result.add(auth);
            }
        }
        return new JsonResult(0,"",result);
    }

    @RequestMapping("/getYukariConcern")
    public JsonResult getYukariConcern(){
        List<Integer> authIdList = permissionService.getAuthIdByPermission(2);
        List<Integer> list = permissionService.getAuthIdByPermission(3);
        ArrayList<JSONObject> result = new ArrayList<>();
        for (Integer authId :
                authIdList) {
            Auth auth = authService.getCacheAuth(authId);
            if (auth != null){
                JSONObject json = new JSONObject();
                json.put("id",auth.getId());
                json.put("name",auth.getName());
                json.put("liwu",list.contains(auth.getId()));
                result.add(json);
            }
        }
        return new JsonResult(0,"",result);
    }


}
